package net.code.btalk.ui;


import net.code.btalk.BTalk;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.AutoTextEditField;
import net.rim.device.api.ui.component.Dialog;

public class MessageTextField extends AutoTextEditField {
	public static BTalk btalk;
	private String placeholder = "write something...";
	
	public MessageTextField(){ 
		super("", "", 200, AutoTextEditField.FIELD_HCENTER);
	}
	
	protected boolean keyChar(char key, int status, int time) {
		if (key == Keypad.KEY_ENTER) {
			if (this.getText().length() > 0) {
				if (btalk.state == BTalk.STATE_ONLINE) {
					btalk.sendMessage(this.getText());
					this.setText("");
				} else {
					Dialog.alert("You are currently not online!");
				}
			}
			return true;
		} else {
			return super.keyChar(key, status, time);
		}
	}
	
	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		int oldColor = g.getColor();
		try {
			g.setColor(0x00000000);
			String test = super.getText();
			if (test == null || test.length() < 1) {
				g.setColor(0x00808080);
				g.drawText(this.placeholder, 0, 0);
			}
			super.paint(g);
		} finally {
			g.setColor(oldColor);
		}
	}

	protected void paintBackground(Graphics g) {
		// TODO Auto-generated method stub
		g.setBackgroundColor(0x00FFFFFF);
		g.clear();
		super.paintBackground(g);
	}
	
}
